<template>
  <div>
    <!-- 
          el-dialog
          center	是否对头部和底部采用居中布局	boolean
       -->
    <el-dialog
      center
      :title="isAdd ? '添加菜单' : '编辑菜单'"
      :visible.sync="isShow"
      :before-close="cancel"
    >
      <el-form :model="mslist" ref="cateInfo" :rules="rules">
        <el-form-item label="活动名称" :label-width="formLabelWidth">
          <el-input v-model="mslist.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" :label-width="formLabelWidth">
          <el-date-picker
            v-model="timer"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeTimer"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类" :label-width="formLabelWidth">
          <el-select
            v-model="mslist.first_cateid"
            placeholder="请选择分类"
            @change="ej"
          >
            <el-option
              v-for="item in cate"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="二级分类"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-select v-model="mslist.second_cateid" placeholder="请选择分类">
            <el-option
              v-for="item in erjifl"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" :label-width="formLabelWidth" prop="title">
          <el-select v-model="mslist.goodsid" placeholder="请选择分类">
            <el-option
              v-for="item in spgl"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="mslist.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { indexRoutes } from "../../router/index";
import { seckadd, seckinfo, seckedit } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      timer: [],
      dialogImageUrl: "",
      dialogVisible: false,
      rules: {
        rolename: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
      mslist: {
        title: "",
        begintime: 0,
        endtime: 0,
        first_cateid: 1,
        second_cateid: 1,
        goodsid: "",
        status: 1,
      },
      defaultProps: {
        children: "children",
        label: "title",
      },
      formLabelWidth: "120px",
      indexRoutes: indexRoutes,
      filelist: [],
      erjifl: [], //二级分类
    };
  },
  computed: {
    ...mapGetters({
      cate: "spec/getcate",
      spgl: "spgl/getSpglList",
    }),
  },

  mounted() {
    this.getSpgl();
    this.getCate();
    this.getMs();
  },
  methods: {
    ...mapActions({
      getCate: "spec/getCateList",
      getSpgl: "spgl/getSpglListAction",
      getMs: "ms/getMsListAction",
    }),
    changeTimer(e) {
      console.log(e);
      this.mslist.begintime = new Date(e[0]).getTime();
      this.mslist.endtime = new Date(e[1]).getTime();
      console.log(this.mslist.endtime);
    
    },
    ej() {
      let index = this.cate.findIndex((item) => {
        return item.id == this.mslist.first_cateid;
      });
      this.erjifl = this.cate[index].children;
    },
    handleRemove(file) {
      console.log(file);
      this.cartlist.img = file.raw;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
    },

    // 清空表单
    reset() {
      (this.mslist = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: 1,
        second_cateid: 1,
        goodsid: "",
        status: "",
      }),
        this.$refs.cateInfo.resetFields();
      this.timer = [];
    },
    //封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    // 封装一个事件
    lookup(id) {
      console.log(id, 111111111111111);
      //  获取一条分类数据
      seckinfo({ id }).then((res) => {
        if (res.code == 200) {
          console.log(res);
          
          this.mslist = res.list;
          this.mslist.id = id;
          console.log(new Date(parseInt(this.mslist.endtime) ),'时间')
          this.timer.push(new Date(parseInt(this.mslist.begintime) ))
          this.timer.push(new Date(parseInt(this.mslist.endtime) ))
    console.log(this.timer);
         
          
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    add() {
      this.$refs.cateInfo.validate((val) => {
        if (val) {
          if (this.isAdd) {
            // 商品添加
            console.log(this.mslist);
            seckadd(this.mslist).then((res) => {
              console.log(this.mslist);
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getMs();
                this.cancel();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            seckedit(this.mslist).then((res) => {
              console.log(this.timer,'timer');
              console.log(res);
              if (res.code == 200) {
                this.$message.success(res.msg);

                this.cancel();
                this.getCate();
              } else {
                this.$message.error(res.msg);
              }
              this.cancel();
            });
          }
        }
      });
    },
  },
};
</script>

<style scoped></style>
